// 获取主表数据逻辑

import ajaxMainTableData from '../../request-functions/getMainTableData';
import deepCopy from '../../../../public/functions/deep-copy';


export default function getMainTableData() {
    const {
        searchModalValue,
        currentSelectDepartment,
        humanResourceOrganizationValue,
        queryOid,
        include_child_orgs,
        pageInfo,
        show_on_job_psn
    } = this.state;

    const {
        editTable,
        button
    } = this.props;

    let postData = {
        querycondition:{
            logic: 'and',
            conditions:[]
        },
        queryAreaCode: "ncc60071025query",
        querytype: "tree", 
        ...searchModalValue,
        pk_keypsn_group: currentSelectDepartment,
        pk_org: humanResourceOrganizationValue.refpk,
        oid: queryOid,
        show_his_key_psn: include_child_orgs,
        showHisGrp: show_on_job_psn,// 显示历史人员组
        pageInfo: pageInfo
    };

    return ajaxMainTableData(postData)
        .then((res) => {

            if (res.success) {
                if (!res.data) {
                    res.data = {
                        'ncc60071025list': {
                            rows: []
                        }
                    };
                }
                res.data['ncc60071025list'].rows.map((item) => {
                    if(item.values['showorder'].value === '9999999') {
                        item.values['showorder'].value = '';
                    }
                });
                const pks = res.data['ncc60071025list'].allpks;
                editTable.setTableData('ncc60071025list', deepCopy(res.data['ncc60071025list']));
                editTable.focusRowByIndex('ncc60071025list', 0);
                let maxLength = Math.max(pageInfo.pageSize, res.data['ncc60071025list'].rows.length);
                maxLength = maxLength < 10 ? 10 : maxLength;
                this.setState({
                    searchModalVisible: false,
                    hasSearched: true,
                    pageInfo: res.data['ncc60071025list'].pageInfo || {
                        pageSize: 10,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    },
                    exportAllpks: pks,
                    mainTableData: res.data['ncc60071025list'],
                    clickRow: {
                        index: 0,
                        record: (res.data['ncc60071025list'] && res.data['ncc60071025list'].rows[0]) ? res.data['ncc60071025list'].rows[0] : null
                    },
                    mainTableHeight: maxLength * 30
                });
                this.setButtonStatus2('mainTableHasData');
                
                // 左侧树
                let {disabledTreeNodeList, currentSelectDepartment} = this.state;
                let currentDisabled = disabledTreeNodeList.includes(currentSelectDepartment);
                this.props.button.setButtonDisabled({
                    // grpadd: false,
                    // grpedit: false,
                    // grpdel: false,
                    // grpref: false,
                    // grpena: !currentDisabled,
                    // grpdis: currentDisabled,
                    Delete: !currentSelectDepartment,
                    stop: !currentSelectDepartment,
                    refadd: !currentSelectDepartment || currentDisabled
                });
                this.doDisabledTreeNodeList();
                // editTable.setAllCheckboxAble('ncc60071025list', !currentDisabled);
            }
        });
}
